package com.ellricky.baselib.ui.xufei

import android.view.View
import android.widget.AdapterView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.StrUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.adapter.CommonGridItemDecoration
import com.ellricky.baselib.adapter.xufei.PayZjsAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.PayZjsEntry
import com.ellricky.baselib.entity.new.StuDetail
import com.ellricky.baselib.entity.xufei.PayZjsEntity
import com.ellricky.baselib.http.Api
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.http.callback.ECallback
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewECallback
import kotlinx.android.synthetic.main.base_activity_stu_detail.*
import java.util.*

class PayZjsDetailActivity : BaseToolBarActivity<Void?, BasePresenter<Void?>?>(),
    AdapterView.OnItemClickListener {
    private var tvZjs: TextView? = null
    private var tvBm: TextView? = null
    private var rvZjs: RecyclerView? = null
    private var rvBm: RecyclerView? = null
    private var zjsAdapter: PayZjsAdapter? = null
    private var bmAdapter: PayZjsAdapter? = null
    private var stuId: String = ""

    override fun getContentLayout(): Int {
        return R.layout.base_activity_stu_zjs_detail
    }

    override fun initUIView() {
        stuId = intent.getStringExtra(BaseConstant.KEY_ID) ?: ""
        super.initUIView()
        mTvTitle.text = "转介绍详情"
        tvZjs = findViewById(R.id.tvZjsNum)
        tvBm = findViewById(R.id.tvBmNum)
        rvZjs = findViewById(R.id.rvZjs)
        rvBm = findViewById(R.id.rvBm)
        initRv()
    }

    private fun initRv() {
        val glm1: GridLayoutManager = object : GridLayoutManager(this, 4) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rvZjs!!.layoutManager = glm1
        rvZjs!!.addItemDecoration(CommonGridItemDecoration(this, 4, 20))
        rvZjs!!.isNestedScrollingEnabled = false
        zjsAdapter = PayZjsAdapter(this)
        zjsAdapter!!.setOnItemClickListener(this)
        rvZjs!!.adapter = zjsAdapter
        val glm2: GridLayoutManager = object : GridLayoutManager(this, 4) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rvBm!!.layoutManager = glm2
        rvBm!!.addItemDecoration(CommonGridItemDecoration(this, 4, 20))
        rvBm!!.isNestedScrollingEnabled = false
        bmAdapter = PayZjsAdapter(this)
        bmAdapter!!.setOnItemClickListener(this)
        rvBm!!.adapter = bmAdapter
        queryData()
    }

    private fun queryData() {
        val url = EAppUtil.getApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = getActions()
        paramMap["member_id"] = stuId
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance().postFrom(url, paramMap, object : NewECallback<PayZjsEntry>(this) {
            override fun onSuccess(data: PayZjsEntry, msg: String?) {
                tvZjs!!.text =
                    getString(R.string.base_zjs_num_str, data.introduction_num)
                tvBm!!.text =
                    getString(R.string.base_bm_num_str, data.apply_num)
                val zjsList = data.introductionNumList
                zjsAdapter!!.refresh(zjsList)
                val bmList = data.applyNumList
                bmAdapter!!.refresh(bmList)
            }
        })
    }

    private fun getActions(): String {
        when (LocalCacheUtil.getCardType()) {
            BaseConstant.CARD_PRESIDENT -> {
                return "headIntroductionDetail"
            }
            BaseConstant.CARD_TEACHER_MANAGER -> {
                return "gzaIntroductionDetail"
            }
            BaseConstant.CARD_TEACHER -> {
                return "introductionDetail"
            }
            BaseConstant.CARD_CONSULTANT -> {
                return "introductionDetail"
            }
        }
        return ""
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) {}
}